<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Dao\UserDao;
use App\Job\Auto\ChatResQueueJob;
use App\Job\Auto\TestChatResQueueJob;
use App\Model\AutoChat;
use App\Model\User;
use App\Model\UserSetting;
use App\Request\IndexRequest;
use App\Services\PushMessageService;
use App\Services\RsaService;
use App\Utils\LoggerUtil;
use App\Utils\RedisClient;
use App\Utils\ToolUtil;
use Hyperf\Contract\ConfigInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use function Hyperf\Support\now;

class IndexController extends BaseController
{

    #[Inject]
    public RsaService $rsaService;

    #[Inject]
    public ConfigInterface $config;


    public function index(IndexRequest $request)
    {
        $userSetting = UserSetting::query()->where('uid', 1000019)->first(['show_addr', 'hidden_nick']);
        if ($userSetting) {
            $show_addr = $userSetting['show_addr'];
        } else {
            $show_addr = 22;
        }
        return [
            'hidden_nick' => $userSetting['hidden_nick'],
            'show_addr' => $show_addr ?? 22
        ];

        $t = AutoChat::query()->insert([
            'uid' => 1,
            'aid' => 2,
        ]);
        return $t;

        // PushMessageService::putYunXinSendMsgQueue(1000015, 1000019, '内推群消息', 1, 3);
        $intervals = [0, 5, 10, 15, 20];
        // foreach ($intervals as $interval) {
        //     $data['uid'] = 1000019;
        //     $data['time'] = $interval;
        //     $t = pushDelayJob(ChatResQueueJob::class, $data, $interval);
        //     var_dump($t);
        // }
        $json = RedisClient::getInstance()->hGet('auto_chat_res', strval(1000019));
        $data = json_decode($json, true);
        $users = $data['users'] ?? [];
        if (count($users) > 5) {
            RedisClient::getInstance()->hDel('auto_chat_res', strval(1000019));
        }
        return $users;
        return $data;
        return $t;

        // 获取所有在线的男性用户
        $onlineUsers = UserDao::getInstance()->getOnlineMaleUids();
        if (!empty($onlineUsers)) {
            $redis = RedisClient::getInstance();
            // 获取所有审核环境的uid
            $auditUids = UserDao::getInstance()->getAuditUidsEx();
            $users = array_diff($onlineUsers, $auditUids);
            foreach ($users as $uid) {
                $rs = $redis->hGet('auto_chat_res', strval($uid));
                $intervals = [0, 10, 20, 30, 40, 120, 180, 240, 300, 360, 420, 480, 540];
                if (empty($rs)) {
                    $data = [
                        'uid' => $uid,
                        'users' => []
                    ];
                    foreach ($intervals as $interval) {
                        $data['time'] = $interval;
                        pushDelayJob(ChatResQueueJob::class, $data, $interval);
                    }
                    $redis->hSet('auto_chat_res', strval($uid), json_encode($data));
                } else {
                    $data = json_decode($rs, true);
                    $users = $data['users'] ?? [];
                    if (count($users) > count($intervals)) {
                        $redis->hDel('auto_chat_res', strval($uid));
                    }
                    LoggerUtil::write('index', 'already exists');
                }
            }
            User::query()->whereIn('id', $onlineUsers)->update(['auto_chat_online' => 0]);
        } else {
            LoggerUtil::write('index', 'no online male users');
        }

    }

    public function refreshLogin()
    {
        LoggerUtil::write('refreshLogin', time(), 'start');
        $url = $this->request->post('url', 'http://47.239.52.125/app/v1/user/login.php');
        $list = Db::connection('linux')->table('tb_user')->pluck('imei', 'id')->toArray();
        foreach ($list as $k => $v) {
            $post = [
                'type' => 5,
                'imei' => $v,
            ];
            ToolUtil::api_curl($url, $post);
        }
        // var_dump($post);
        LoggerUtil::write('refreshLogin', time(), 'end');
    }

    public function encode()
    {
        $params = $this->request->post();
        $serverE = $this->rsaService->serverEncode(json_encode($params));
        $clientD = $this->rsaService->clientDecode($serverE);
        $clientE = $this->rsaService->clientEncode(json_encode($params));
        $serveD = $this->rsaService->serverDecode($clientE);

        return $this->response->json([
            'serverE' => $serverE,
            'clientD' => $clientD,
            'clientE' => $clientE,
            'serveD' => $serveD,
        ]);

    }

    public function decode()
    {
        $data = $this->request->post('data');
        $clientD = $this->rsaService->clientDecode($data);
        return $this->response->json([
            'clientD' => $clientD,
        ]);

    }

    public function sdecode()
    {
        $data = $this->request->post('data');
        $clientD = $this->rsaService->serverDecode($data);
        return $this->response->json([
            'clientD' => $clientD,
        ]);

    }


    public function logger()
    {
        LoggerUtil::write('logger', now()->toDateTimeString());
    }

}
